package cn.linpq.panel;

import cn.linpq.frame.MineFrame;
import cn.linpq.util.Tools;
import sun.dc.pr.PRError;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Desc : 状态栏类
 * ClassName : cn.linpq.panel.MineState
 *
 * @author : 林鹏群
 * @date : 2022/12/12 11:32
 */
public class MineState extends JPanel {
    private MineFrame mineFrame;
    private JLabel mineCountHundreds;
    private JLabel mineCountTens;
    private JLabel mineCountSingle;
    private JLabel face;
    private JLabel timingHundreds;
    private JLabel timingTens;
    private JLabel timingSingle;
    private Timer timer;
    private int flagCount;

    /**
     * 初始化
     */
    public MineState(MineFrame mineFrame) {
        flagCount = Tools.totalMines;
        this.mineFrame = mineFrame;
        //初始化所有属性
        this.mineCountHundreds = new JLabel(Tools.count[0]);
        this.mineCountTens = new JLabel(Tools.count[0]);
        this.mineCountSingle = new JLabel(Tools.count[0]);
        this.face = new JLabel(Tools.faces[0]);
        this.timingHundreds = new JLabel(Tools.count[0]);
        this.timingTens = new JLabel(Tools.count[0]);
        this.timingSingle = new JLabel(Tools.count[0]);
        //设置边框样式[1、内边框凹陷:createLoweredBevelBorder；2、外边框边距5:createEmptyBorder；3、内外边框组合:createCompoundBorder]
        Border borderIn = BorderFactory.createLoweredBevelBorder();
        Border borderOut = BorderFactory.createEmptyBorder(5, 5, 5, 5);
        Border border = BorderFactory.createCompoundBorder(borderOut, borderIn);
        this.setBorder(border);
        //设置背景颜色
        this.setBackground(Color.LIGHT_GRAY);
        //设置布局
        this.setLayout(new BorderLayout());
        //创建Box[采用Box水平布局，依次添加水平createHorizontalStrut、雷数百位、雷数十位、雷数各位、createHorizontalGlue、笑脸、createHorizontalGlue、计时百位、计时十位、计时各位、createHorizontalStrut]
        Box box = Box.createHorizontalBox();
        box.add(Box.createHorizontalStrut(5));
        box.add(mineCountHundreds);
        box.add(mineCountTens);
        box.add(mineCountSingle);
        box.add(Box.createHorizontalGlue());
        box.add(face);
        box.add(Box.createHorizontalGlue());
        box.add(timingHundreds);
        box.add(timingTens);
        box.add(timingSingle);
        box.add(Box.createHorizontalStrut(5));
        //添加box
        this.add(box);
        this.setMineCount(Tools.totalMines);
        //初始化Timer[每秒修改计数]
        timer = new Timer(Tools.delay, new ActionListener() {
            int time = 0;

            @Override
            public void actionPerformed(ActionEvent e) {
                time++;
                setTime(time);
            }
        });
        face.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                JLabel source = (JLabel) e.getSource();
                source.setIcon(Tools.faces[1]);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                mineFrame.restart();
            }
        });
    }


    public void setTime(int time) {
        timingHundreds.setIcon(Tools.count[time / 100 % 10]);
        timingTens.setIcon(Tools.count[time / 10 % 10]);
        timingSingle.setIcon(Tools.count[time % 10]);
    }

    public void setMineCount(int mineCount) {
        if (mineCount >= 0) {
            mineCountHundreds.setIcon(Tools.count[mineCount / 100 % 10]);
            mineCountTens.setIcon(Tools.count[mineCount / 10 % 10]);
            mineCountSingle.setIcon(Tools.count[mineCount % 10]);
        }
    }

    public MineFrame getMineFrame() {
        return mineFrame;
    }

    public void setMineFrame(MineFrame mineFrame) {
        this.mineFrame = mineFrame;
    }

    public JLabel getMineCountHundreds() {
        return mineCountHundreds;
    }

    public void setMineCountHundreds(JLabel mineCountHundreds) {
        this.mineCountHundreds = mineCountHundreds;
    }

    public JLabel getMineCountTens() {
        return mineCountTens;
    }

    public void setMineCountTens(JLabel mineCountTens) {
        this.mineCountTens = mineCountTens;
    }

    public JLabel getMineCountSingle() {
        return mineCountSingle;
    }

    public void setMineCountSingle(JLabel mineCountSingle) {
        this.mineCountSingle = mineCountSingle;
    }

    public JLabel getFace() {
        return face;
    }

    public void setFace(JLabel face) {
        this.face = face;
    }

    public JLabel getTimingHundreds() {
        return timingHundreds;
    }

    public void setTimingHundreds(JLabel timingHundreds) {
        this.timingHundreds = timingHundreds;
    }

    public JLabel getTimingTens() {
        return timingTens;
    }

    public void setTimingTens(JLabel timingTens) {
        this.timingTens = timingTens;
    }

    public JLabel getTimingSingle() {
        return timingSingle;
    }

    public void setTimingSingle(JLabel timingSingle) {
        this.timingSingle = timingSingle;
    }

    public Timer getTimer() {
        return timer;
    }

    public void setTimer(Timer timer) {
        this.timer = timer;
    }
}
